package com.hwua.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author CQY
 */
public class DBHelper {

    /**
     * 驱动
     */
    public static String driver;
    /**
     * 地址
     */
    public static String url;
    /**
     * 用户名
     */
    public static String user;
    /**
     * 密码
     */
    public static String pwd;

    //静态代码块，加载资源
    static {
        Properties pro = new Properties();
        InputStream in = DBHelper.class.getClassLoader().getResourceAsStream("jdbc.properties");

        try {
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver = pro.getProperty("driver");
        url = pro.getProperty("url");
        user = pro.getProperty("user");
        pwd = pro.getProperty("pwd");

    }

    public static Connection getConnection() {
        //加载驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("驱动加载失败！");
        }
        //
        Connection conn= null;
        try {
            conn = DriverManager.getConnection(url,user,pwd);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库连接失败！");
        }
        return conn;
    }


    /**
     * 释放资源，关闭连接等
     *
     * @param rs
     * @param pre
     * @param conn
     */
    public static void close(ResultSet rs, PreparedStatement pre, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (pre != null) {
            try {
                pre.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭资源 非查询的
     *
     * @param pre
     * @param conn
     */
    public static void close(PreparedStatement pre, Connection conn) {
        if (pre != null) {
            try {
                pre.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 增删改封装
     * @param sql:要执行的sql语句
     * @param params
     * sql对应的参数值 必须和问号一一对应
     * @return
     * @throws SQLException
     */
    public static int update(String sql, Object[] params) throws SQLException {
        // 1.注册数据库驱动 2.与数据库建立连接
        Connection conn = DBHelper.getConnection();
        // 3.创建用来执行sql语句的PreparedStatement对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        // 4.执行sql语句
        // 先设置占位符的值
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        int rows = pstmt.executeUpdate();
        // 6.释放资源
        DBHelper.close(pstmt,conn);
        return rows;
    }


    public static ResultSet query(String sql, Object[] params) throws SQLException {
        // 1.注册数据库驱动 2.与数据库建立连接
        Connection conn = DBHelper.getConnection();
        // 3.创建用来执行sql语句的PreparedStatement对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        // 4.执行sql语句
        // 先设置占位符的值
        if (params != null && params.length > 0) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
        ResultSet resultSet=pstmt.executeQuery();
        return resultSet;
    }


}
